
import BL.Eintrag;

import BL.AccessDB;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.jmx.snmp.Timestamp;

/**
 * Servlet implementation class DatenbankZugriff
 */
@WebServlet("/DatenbankZugriff")
public class DatenbankZugriff extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DatenbankZugriff() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		double lat = 41.0;
		if( !(request.getParameter("lat").equals("undefined")) ){
			lat = Double.parseDouble(request.getParameter("lat"));
		}
		double lng = 9.0;
		if( !(request.getParameter("lng").equals("undefined")) ){
			lng = Double.parseDouble(request.getParameter("lng"));
		}
		

		String kat = request.getParameter("kat");
		String aTime = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(new Date());
		aTime = aTime.substring(11,16);
		aTime = aTime.substring(0,2)+aTime.substring(3);	
		int time = Integer.parseInt(aTime);
		
		SimpleDateFormat formata = new SimpleDateFormat("EEEE"); // EEEE = WOCHENTAG
		formata.setTimeZone(TimeZone.getTimeZone("CET"));
		String day = formata.format(new Date());
		int iDay;
		
		if(day.equalsIgnoreCase("Montag")){
			iDay=1;
		}else if(day.equalsIgnoreCase("Dienstag")){
			iDay=5;
		}else if(day.equalsIgnoreCase("Mittwoch")){
			iDay=9;
		}else if(day.equalsIgnoreCase("Donnerstag")){
			iDay=13;
		}else if(day.equalsIgnoreCase("Freitag")){
			iDay=17;
		}else if(day.equalsIgnoreCase("Samstag")){
			iDay=21;
		}else{
			iDay=25;
		}
		//System.out.println(iDay);
		
		
		Eintrag[] erg;
		try {
			erg = AccessDB.getErgebnisliste(kat, lat, lng, time, iDay);
			HttpSession session = request.getSession();
			session.setAttribute("Ergebnis", erg);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
	}
	


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
